module ui {

    export class TxtCountDown extends egret.DisplayObjectContainer {

        public static readonly COUNT_DOWN_FINISH: string = "count_down_finish";

        private secondsTxt: egret.TextField;

        private timer: egret.Timer;

        private current: number = 30;

        public constructor(txtSeconds: egret.TextField = null) {
            super();
            if (txtSeconds == null) {
                this.secondsTxt = new egret.TextField();
                this.secondsTxt.textColor = 0x0;
                this.secondsTxt.size = 50;
                this.addChild(this.secondsTxt);
            } else {
                this.secondsTxt = txtSeconds;
            }
            this.timer = new egret.Timer(1000);
            this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
        }

        public start(current: number): void {
            this.current = current;
            this.secondsTxt.text = this.current + "S";
            this.timer.start();
        }

        public stop(): void {
            this.timer.reset();
        }

        public pause(): void {
            this.timer.stop();
        }

        public resume(): void {
            this.timer.start();
        }

        public getCostSeconds(): number {
            return this.current;
        }

        private onTimer(eve: egret.Event): void {
            if (this.current < 1) {
                egret.log("倒计时结束");
                this.timer.reset();
                this.dispatchEvent(new egret.Event(TxtCountDown.COUNT_DOWN_FINISH,
                    false, false, null));
                return;
            }
            this.current--;
            this.secondsTxt.text = this.current + "S";
        }


    }

}